package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.dto.BedDtoId;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.mapper.RoomTypeMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }
    /**
     * 新增床位
     *
     * @param bedDto
     * @return
     */
    @Transactional
    @Override
    public void createBed(BedDto bedDto) {
        // todo 新增床位床位好不能重复
        Bed bed1 = bedMapper.selectByNumber(bedDto.getBedNumber());
        if (bed1 != null) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        log.info("日志 dto{}",bedDto);
        //给数据库新增床位信息
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        //完善信息  id自增 \床位状态0  备注为nyull
        bed.setBedStatus(0);

        try {
            bedMapper.createBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        log.info("日志 新增床位成功");
        //新增床位  房型的数量也要增加  现根据房见id 查出房型名称 根据房型名称增加房型的数量

        //根据id查询房型名称
        Room     room =  roomMapper.selectById(bed.getRoomId());
        String typeName = room.getTypeName();
        log.info("日志房型名称为：{}",typeName);

        //根据房间名称修改房间数量+1
        roomTypeMapper.updateName(typeName);
        log.info("日志房间数量修改成功");




    }
    /**
     * 根据id查询房间信息
     * @param id
     * @return
     */
    @Override
    public BedVo readBedById(Integer id) {

        Bed bed = bedMapper.selectById(id);
        log.info("日志===查询房间信息成功{}",bed);
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bed,bedVo);
        return bedVo;
    }
    /**
     * 根据id修改床位信息
     * @param bedDto
     * @return
     */
    @Override
    public void updateBed(BedDto bedDto) {
        Integer bed1 = bedMapper.selectByNumberId(bedDto.getBedNumber(), bedDto.getId());
        //  新增床位床位好不能重复
        if (bed1 > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }

        //  todo 判断状态为0 才能修改
        Bed bed2 = bedMapper.selectById(Math.toIntExact(bedDto.getId()));
        if (bed2.getBedStatus() != 0){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_OCCUPIED);
        }
        log.info("日志===修改床位信息{}",bedDto);
        bedMapper.updateBedId(bedDto);
        log.info("修改床位信息成功");

    }




    /**
     * 根据id删除床位信息
     * @param id
     */
    @Override
    public void deleteBed(Long id) {
        Bed bed = bedMapper.selectById(Math.toIntExact(id));
        if (bed !=null){
            bedMapper.deleteBed(id);
            log.info("删除床位信息成功");
            // todo 删除床位  房型的数量也要减少

            //根据id查询房型名称
            Room     room =  roomMapper.selectById(bed.getRoomId());
            String typeName = room.getTypeName();
            log.info("日志房型名称为：{}",typeName);

            //根据房间名称修改房间数量-1
            roomTypeMapper.updateNameJ(typeName);
            log.info("日志房间数量修改成功");
        }


    }

/*    @Override
    public void updateBed(BedDto bedDto) {
        BedVo bedVo = getBedById(bedDto.getId());
        bedVo.setSort(bedDto.getSort());
        bedVo.setBedNumber(bedDto.getBedNumber());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) {
            bedVo.setBedStatus(bedDto.getBedStatus());
        }
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bedMapper.updateBed(bed);
    }*/



}

